/**
 * 
 */
package com.wsj.web.subclass.function;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.tool.HtmlCreateObj;
import com.wsj.util.tool.RequestUtils;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;
import com.wsj.web.vo.homeSite.HomeSiteNews;

/**
 * @author super
 *
 */
public class HomeSiteNewsCreateHtmlObj implements Runnable
{

	//主站资讯接口
	 private IHomeSiteNewsServ  homeSiteNewsServ;
	 
		//	生成html页面对象
		private HtmlCreateObj htmlCreateObj;
		
		private HttpServletRequest request;
		private HttpServletResponse response;
		
		private HomeSiteNews  homeSiteNews;
	
	/****
	 * 构造函数
	 */
	public HomeSiteNewsCreateHtmlObj(IHomeSiteNewsServ  homeSiteNewsServ,HtmlCreateObj htmlCreateObj,HttpServletRequest request,HttpServletResponse response,HomeSiteNews homeSiteNews)
	{
	   this.homeSiteNewsServ =homeSiteNewsServ;
	   this.htmlCreateObj = htmlCreateObj;
	   this.request = request;
	   this.response  =response;
	   this.homeSiteNews = homeSiteNews;
	}
	
	public void run() 
	{
		createFile();
	}
	
	/****
	 * 生成静态页面.
	 *
	 */
	public synchronized void createFile() 
	{
		try 
		{
			String filePath = "";
			String spliter="<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>";
	    	List<String> contentslist=new ArrayList<String>();
			String con=homeSiteNews.getContent();
			if(con!=null&&!con.equals("")&&con.indexOf(spliter)!=-1){
				String[] contents=con.split(spliter);
				if(contents!=null&&contents.length>0){
					for(int i=0;i<contents.length;i++){
						if(contents[i]!=null&&!contents.equals("")){
							contentslist.add(contents[i]);
						}
					}
				}
			}else 
			{
				  //生成静态页面
				 filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(1)+".html";
				htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId(), "html/news/", filePath);
//				 生成静态页面
				htmlCreateObj.createHtmlFile(request, response);
		        //设置静态文件路径
				homeSiteNews.setFilePath(filePath);
				this.homeSiteNewsServ.update(homeSiteNews);
			}
			if(contentslist!=null&&!contentslist.isEmpty()&&contentslist.size()>0)
			{
				for(int i=0;i<contentslist.size();i++){
					if(i==0){
				        //生成静态页面
						 filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
						htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
//						 生成静态页面
						htmlCreateObj.createHtmlFile(request, response);
				        //设置静态文件路径
						homeSiteNews.setFilePath(filePath);
						this.homeSiteNewsServ.update(homeSiteNews);
						
					}else 
					{
				        //生成静态页面
						 filePath=HomeSiteNewsConstant.homeSiteNewsHead+homeSiteNews.getId()+"_"+String.valueOf(i+1)+".html";
						htmlCreateObj.setCreateFileParament(RequestUtils.getRequestHost(request)+HomeSiteNewsConstant.homeSiteNewsPath+homeSiteNews.getId()+"&content="+i, "html/news/", filePath);
//						 生成静态页面
						htmlCreateObj.createHtmlFile(request, response);
					}
				}
				
			}
			
			
			
      } catch (BusinessException e) 
      {
	    e.printStackTrace();
      }
     
	}

	public IHomeSiteNewsServ getHomeSiteNewsServ() {
		return homeSiteNewsServ;
	}

	public void setHomeSiteNewsServ(IHomeSiteNewsServ homeSiteNewsServ) {
		this.homeSiteNewsServ = homeSiteNewsServ;
	}
	

}
